package com.example.controller;

import com.example.entity.*;
import com.example.service.AirLaneService;
import com.example.service.OperationRecordService;
import com.example.service.TakeoffLandService;
import com.example.service.WorkService;
import com.example.util.MicroResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Controller
public class AirspaceManageController {

    @Autowired
    private AirLaneService airLaneService;
    @Autowired
    private TakeoffLandService takeoffLandService;
    @Autowired
    private WorkService workService;
    @Autowired
    private OperationRecordService operationRecordService;

    @PostMapping("/module6/airlanestatus")
    public ResponseEntity update(@RequestParam Integer id, @RequestBody AirLane airLane) {
        // 确保更新的用户具有正确的 ID
        airLane.setId(id);
        // 调用服务层方法进行更新
        Optional<AirLane> updatedOptional = airLaneService.update2(id, airLane); // 返回所有字段数据
        // 根据更新结果返回响应
        if (updatedOptional.isPresent()) {
            return ResponseEntity.ok(MicroResponseUtils.success(updatedOptional.get()));
        } else {
            return ResponseEntity.ok(MicroResponseUtils.error("Update failed or AirLane not found")); // 或者返回适当的错误响应
        }
    }

    public ResponseEntity update(@RequestParam Integer id,
                                 @RequestParam(required = false) String startstopstate,
                                 @RequestParam(required = false) String correspondingtimeperiod,
                                 @RequestParam(required = false) Float capacitythreshold,
                                 @RequestParam(required = false) Float safeintervalthreshold){
        Optional<AirLane> updatedOptional = airLaneService.updateParam(id,
                startstopstate,
                correspondingtimeperiod,
                capacitythreshold,
                safeintervalthreshold);
        if (updatedOptional.isPresent()) {
            return ResponseEntity.ok(MicroResponseUtils.success(updatedOptional.get()));
        } else {
            return ResponseEntity.ok(MicroResponseUtils.error("Update failed or AirLane not found"));
        }
    }

    @PostMapping("/module6/takeoffstatus")
    public ResponseEntity update(@RequestParam Integer id, @RequestBody TakeoffLand takeoffLand) {
        // 确保更新的用户具有正确的 ID
        takeoffLand.setId(id);
        // 调用服务层方法进行更新
        Optional<TakeoffLand> updatedOptional = takeoffLandService.update2(id, takeoffLand);
        // 根据更新结果返回响应
        if (updatedOptional.isPresent()) {
            return ResponseEntity.ok(MicroResponseUtils.success(updatedOptional.get()));
        }else {
            return ResponseEntity.ok(MicroResponseUtils.error("Update failed or TakeoffLand not found"));
        }
    }

    @PostMapping("/module6/workstatus")
    public ResponseEntity update(@RequestParam Integer id, @RequestBody Work work) {
        // 确保更新的用户具有正确的 ID
        work.setId(id);
        // 调用服务层方法进行更新
        Optional<Work> updatedOptional = workService.update2(id, work);
        // 根据更新结果返回响应
        if (updatedOptional.isPresent()) {
            return ResponseEntity.ok(MicroResponseUtils.success(updatedOptional.get()));
        }else {
            return ResponseEntity.ok(MicroResponseUtils.error("Update failed or Work not found"));
        }
    }

    @GetMapping("/module6/airlane")
    public ResponseEntity getAllAirlane() {
        return ResponseEntity.ok(MicroResponseUtils.success(airLaneService.findAll()));
    }

    @GetMapping("/module6/takoffland")
    public ResponseEntity getAllTakeoffLand() {
        return ResponseEntity.ok(MicroResponseUtils.success(takeoffLandService.findAll()));
    }

    @GetMapping("/module6/work")
    public ResponseEntity getAllWork() {
        return ResponseEntity.ok(MicroResponseUtils.success(workService.findAll()));
    }

    @PostMapping("/module6/search")
    public ResponseEntity search(
            @RequestParam String keyword,
            @RequestParam(required = false, defaultValue = "airspace_object_name") String sortedField,
            @RequestParam(required = false, defaultValue = "true") Boolean sortedOrder) {
        List<AirLane> searchsorted = airLaneService.searchsorted(keyword, sortedField, sortedOrder);
        return ResponseEntity.ok(MicroResponseUtils.success(searchsorted));
    }

    @PostMapping("/module6/searchall")
    public ResponseEntity searchall(
            @RequestParam String keyword,
            @RequestParam(required = false, defaultValue = "airspace_object_name") String sortedField,
            @RequestParam(required = false, defaultValue = "true") Boolean sortedOrder) {
        List<Object> searchsorted2 = airLaneService.searchsorted2(keyword, sortedField, sortedOrder);
        return ResponseEntity.ok(MicroResponseUtils.success(searchsorted2));
    }

    @PostMapping("/module6/updatetable")
    public ResponseEntity updateTable(@RequestParam String tablename,
                                      @RequestParam Integer id,
                                      @RequestParam(required = false) String startstopstate,
                                      @RequestParam(required = false) String correspondingtimeperiod,
                                      @RequestParam(required = false) Float capacitythreshold,
                                      @RequestParam(required = false) Float safeintervalthreshold){
        Optional<?> update = airLaneService.updateall(tablename, id,
                startstopstate, correspondingtimeperiod, capacitythreshold, safeintervalthreshold);
        if (update.isPresent()) {
            return ResponseEntity.ok(MicroResponseUtils.success(update.get()));
        }
        else {
            return ResponseEntity.ok(MicroResponseUtils.error("Update failed or AirLane not found"));
        }
    }

    @PostMapping("/module6/saveorinsert")
    public ResponseEntity saveorinsert(@RequestBody OperationRecord operationRecord){
        if(operationRecord.getOperationId()==null){
            OperationRecord createdUser = operationRecordService.isave(operationRecord);
            return ResponseEntity.ok(MicroResponseUtils.success(createdUser));
        }
        else {
            Optional<OperationRecord> updatedUser = operationRecordService.updatenew(operationRecord);
            if (updatedUser.isPresent()) {
                return ResponseEntity.ok(MicroResponseUtils.success(updatedUser.get()));
            }
            else {
                return ResponseEntity.ok(MicroResponseUtils.error("Update failed or OperationRecord not found"));
            }
        }
    }

    @PostMapping("/module6/delete")
    public ResponseEntity delete(@RequestBody OperationRecord operationRecord){
        boolean isDeleted = operationRecordService.delete(operationRecord);
        return isDeleted ? ResponseEntity.ok(MicroResponseUtils.success("User deleted successfully")) : ResponseEntity.ok(MicroResponseUtils.error("User not found"));
    }

    @PostMapping("/module6/searchrecord")
    public ResponseEntity searchrecord(@RequestParam Integer id,
                                       @RequestParam String airspaceObjectName){
        List<OperationRecord> records = operationRecordService.findByIdAndAirspaceObjectName(id, airspaceObjectName);
        if (!records.isEmpty()) {
            return ResponseEntity.ok(MicroResponseUtils.success(records));
        }
        else {
            return ResponseEntity.ok(MicroResponseUtils.error("Record not found"));
        }
    }
}
